package org.i77soft.games.MineAcerLite;

import org.i77soft.games.LGame.engine.AppConfig;
import org.loon.framework.android.game.LGameAndroid2DActivity;
import org.loon.framework.android.game.LGameAndroid2DView;
import org.loon.framework.android.game.LMode;

import android.graphics.Rect;
import android.util.Log;
import android.view.Window;
import android.widget.FrameLayout;

public class Main extends LGameAndroid2DActivity {
    private static final int DEBUG = 3;
    public static AppConfig AppConfig;
    private LGameAndroid2DView view;

    public void onMain() {
        // setContentView(R.layout.main);
        AppConfig = new AppConfig(this);
        this.maxScreen(AppConfig.getScreenWidth(), AppConfig.getScreenHeight());
        // this.maxScreen(1280, 720);
        // this.maxScreen(480, 320);
        // this.maxScreen(320, 240);
        // this.maxScreen(320, 480);

        // this.initialization(AppConfig.getLanscape(), LMode.FitFill);
        this.initialization(true, LMode.FitFill);
        this.setShowLogo(false);
        this.setShowMemory(false);
        this.setShowFPS(true);
        this.setScreen(new MineSweeper());
        this.showScreen();
        Log.println(DEBUG, "MainActivity", "========= onMain() =========");

        /*
        view.post(new Runnable()
        {
            public void run()
            {
                viewInited();
            }
        });
        //*/
    }

    final protected void onDraw() {
        Log.println(DEBUG, "MainActivity", "========= onDraw() =========");
    }

    final protected void onSizeChanged() {
        Log.println(DEBUG, "MainActivity", "========= onSizeChanged() =========");
    }

    final protected void onStart() {
        super.onStart();
        Log.d("MainActivity", "========= onStart() =========");
        FrameLayout.LayoutParams linearParams = (FrameLayout.LayoutParams) getFrameLayout()
                .getLayoutParams();
        Log.d("MainActivity", "linearParams.width = " + linearParams.width);
        Log.d("MainActivity", "linearParams.height = " + linearParams.height);
        Log.d("MainActivity", "getWidth() = " + gameView().getGameHandler().getWidth());
        Log.d("MainActivity", "getHeight() = " + gameView().getGameHandler().getHeight());
        Log.d("MainActivity", "getMaxWidth() = " + gameView().getGameHandler().getMaxWidth());
        Log.d("MainActivity", "getMaxHeight() = " + gameView().getGameHandler().getMaxHeight());
        // linearParams.height = 600;
        getFrameLayout().setLayoutParams(linearParams);
    }

    public void onGamePaused() {
        Log.println(DEBUG, "MainActivity", "========= onGamePaused() =========");
        if (AppConfig != null) {
            AppConfig.getScreenSize(this);
        }        
    }

    public void onGameResumed() {
        Log.println(DEBUG, "MainActivity", "========= onGameResumed() =========");
        if (AppConfig != null) {
            AppConfig.getScreenSize(this);
        }
    }

    private void viewInited() {
        try {
            Thread.sleep(1000);
            Rect rect = new Rect();
            Window window = getWindow();
            view.getWindowVisibleDisplayFrame(rect);
            int statusBarHeight = rect.top;
            int contentViewTop = statusBarHeight;
            //contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
            int titleBarHeight = contentViewTop - statusBarHeight;
            // 测试结果：ok之后 100多 ms 才运行了
            Log.v("test", "=-init-= statusBarHeight=" + statusBarHeight +
                    " contentViewTop=" + contentViewTop +
                    " titleBarHeight=" + titleBarHeight);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            //
        }
    }

}
